package com.ruge.system.service.impl;

import com.ruge.common.util.DateUtil;
import com.ruge.common.util.GsonUtil;
import com.ruge.system.pojo.entity.SysLogOper;
import com.ruge.system.pojo.request.LogOperRequest;
import com.ruge.system.pojo.vo.LogOperVo;
import com.ruge.system.repository.SysLogOperRepository;
import com.ruge.system.service.SysLogOperService;
import org.springframework.data.domain.*;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.criteria.Predicate;
import java.util.List;
import java.util.Map;

/**
 * @author ：爱丽丝、如歌
 * @time ：2020/3/21  12:25
 * 描述 ：
 */
@Service
public class SysLogOperServiceImpl implements SysLogOperService {
    @Resource
    private SysLogOperRepository sysLogOperRepository;

    /**
     * 数据查询接口
     *
     * @param request
     * @return Page<LogOperVo>
     */
    @Override
    public Page<LogOperVo> findAll(LogOperRequest request) {
        Specification<SysLogOper> specification = (root, query, cb) -> {
            /**
             * 获取条件对象
             */
            Predicate conjunction = cb.conjunction();
            if (request != null) {
                if (null != request.getId()) {
                    conjunction.getExpressions().add(cb.equal(root.get("id"), request.getId()));
                }
                if (null != request.getUserName()) {
                    conjunction.getExpressions().add(cb.equal(root.get("createBy"), request.getUserName()));
                }
                if (null != request.getStartTime()) {
                    conjunction.getExpressions().add(cb.greaterThanOrEqualTo(root.get("lastUpdateTime"), request.getStartTime()));
                }
                if (null != request.getEndTime()) {
                    conjunction.getExpressions().add(cb.lessThanOrEqualTo(root.get("lastUpdateTime"), request.getEndTime()));
                }
            }
            return conjunction;
        };
        Sort sort = Sort.by(Sort.Order.desc("id"));
        Pageable pageable = PageRequest.of(request != null ? request.getPage() : 0, request != null ? request.getLimit() : 15, sort);
        Page<SysLogOper> page = sysLogOperRepository.findAll(specification, pageable);
        List<LogOperVo> gson = GsonUtil.GsonToListVo(page.getContent(), LogOperVo.class);
        gson.forEach(e->{
            e.setUseTime(DateUtil.millisToStringS(Long.parseLong(e.getUseTime())));
        });
        return new PageImpl<>(gson, pageable, page.getTotalElements());
    }

    /**
     * 数据保存接口
     *
     * @param entity
     * @return SysLogOper
     */
    @Override
    public SysLogOper save(SysLogOper entity) {
        return sysLogOperRepository.save(entity);
    }

    /**
     * 数据删除接口
     *
     * @param id id
     */
    @Override
    public void deleteById(Long id) {
        sysLogOperRepository.deleteById(id);
    }

    /**
     * 批量操作接口
     *
     * @param map Map<String, List<SysLogOper>> map
     * @return List<SysLogOper>
     */
    @Override
    public List<SysLogOper> batch(Map<String, List<SysLogOper>> map) {
        return sysLogOperRepository.batch(map);
    }
}
